package com.example.myapplication.acty;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.chaychan.library.BottomBarLayout;
import com.example.myapplication.R;
import com.example.myapplication.fragment.HomeFragment;
import com.example.myapplication.fragment.MineFragment;

import org.jetbrains.annotations.NotNull;

import java.util.LinkedList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<Fragment> fragments = new LinkedList<>();
    ViewPager viewpage;
    BottomBarLayout bbl;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initBottom();
    }

    void initView() {


    }

    //初始化底部导航栏
    private void initBottom() {
        viewpage = findViewById(R.id.viewpage);
        bbl = findViewById(R.id.bbl);
        fragments.add(new HomeFragment());
        fragments.add(new MineFragment());
        HomeAdapter adapter = new HomeAdapter(getSupportFragmentManager());
        viewpage.setAdapter(adapter);
        //设置viewpage页面保留数量
        viewpage.setOffscreenPageLimit(4);
        bbl.setViewPager(viewpage);
        viewpage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    //viewPage适配器
    class HomeAdapter extends FragmentPagerAdapter {

        public HomeAdapter(@NonNull @NotNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @NotNull
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }
}